package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.stream.events.Attribute;

public class DWPricebook extends DWExportXML {
	
	private String pricebookId;
	
	public DWPricebook (String xmlFile, String pricebookId) {
		super (xmlFile);
		this.pricebookId = pricebookId;
	}

	public void StartXmlOutput() {
		super.StartXmlOutput();
		Attribute attribute;
		List <Attribute>attributeList = new ArrayList<Attribute>(); 
		//= eventFactory.createAttribute("","","catalog-id", inventoryId);
		//List <Attribute>attributeList = new ArrayList<Attribute>(); //		Arrays.asList(attribute);
		//attributeList.add(attribute);
		List <?>nodeList = Arrays.asList();
		attribute = eventFactory.createAttribute("","","xmlns", "http://www.demandware.com/xml/impex/pricebook/2006-10-31");
		attributeList.add(attribute);
		
		DWObject dwo = new DWObject("header");
		dwo.setAttribute("pricebook-id", pricebookId);
		dwo.addChildObject("currency", "USD");
		dwo.addChildObject("online-flag", "true");
		
		
		try {
			eventWriter.add(eventFactory.createStartElement("","", "pricebooks",attributeList.iterator(),nodeList.iterator()));
			eventWriter.add(eventFactory.createDTD("\n"));
			eventWriter.add(eventFactory.createStartElement("","", "pricebook"));
			eventWriter.add(eventFactory.createDTD("\n"));
			writeNode(dwo);
			eventWriter.add(eventFactory.createDTD("\n"));
			eventWriter.add(eventFactory.createStartElement("","", "price-tables"));
		} catch (Exception e) {
			System.err.println("Exception DWInventory.java StartXmlOutput()");
			System.out.println(e);
		}
	}
	
	public void FinishXmlOutput() {
		try {
			eventWriter.add(eventFactory.createDTD("\n"));
			eventWriter.add(eventFactory.createEndElement("", "", "price-tables"));
			eventWriter.add(eventFactory.createDTD("\n"));
			eventWriter.add(eventFactory.createEndElement("", "", "pricebook"));
			eventWriter.add(eventFactory.createDTD("\n"));
			eventWriter.add(eventFactory.createEndElement("", "", "pricebooks"));
		} catch (Exception e) {
			System.err.println("Exception DWInventory.java FinishXmlOutput()");
			System.out.println(e);
		}
		super.FinishXmlOutput();
	}
	
	public DWObject getPricebookDWObject (CSVRecord record, MappingBase fMapping) {
		DWObject dwo = new DWObject("price-table");

		String v;
	
		v = record.getFieldValue( fMapping.mapField("product-id") );
		dwo.setAttribute("product-id", v );

		v = record.getFieldValue( fMapping.mapField("amount") );
		DWObject amount=dwo.addChildObject("amount",v);
		amount.setAttribute("quantity", "1");
/*		
		v = record.getFieldValue( fMapping.mapField("long-description") );
		v = fMapping.mapValue("long-description", v);
		dwo.addChildObject("long-description", v);
		
		dwo.addChildObject("online-flag",	fMapping.mapValue("online-flag",record));
	
		System.out.println("magento field for \"online-flag\" is:"+fMapping.mapField("online-flag"));
		System.out.println("map 1 to dw format is:"+fMapping.mapValue("online-flag","1"));
*/
		return dwo;	
		
	}
	
}
